//
//  SimEmptyView.m
//  Knowbox
//
//  Created by LiuXubin on 15/11/30.
//  Copyright © 2015年 knowin. All rights reserved.
//

#import "SimEmptyView.h"
#import "SimDefine.h"
#import "UIView+SimAdditions.h"

#define kSimEmptyIconTextGap 15

SimImpValue(kSimEmptyViewTitleColor, UIColor *);

@interface SimEmptyView ()
{
}

@property (nonatomic, strong) SimEmptyTouchBlock touchBlock;
@property (nonatomic, strong) UILabel *emptyLb;
@property (nonatomic, strong) UIImageView *iconView;

@end

@implementation SimEmptyView

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _contentOffset = 0;
        self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    }
    
    return self;
}

+ (void)removeFromView:(UIView *)view
{
    for (UIView *subView in [view subviews]) {
        if ([subView isKindOfClass:[SimEmptyView class]]) {
            [(SimEmptyView *)subView setTouchBlock:nil];
            [subView removeFromSuperview];
        }
    }
}

+ (id)showInView:(UIView *)view iconName:(NSString *)iconName content:(NSString *)content touch:(SimEmptyTouchBlock)touchBlock
{
    [SimEmptyView removeFromView:view];
    
    SimEmptyView *emptyView = [[SimEmptyView alloc] initWithFrame:view.bounds];
    [emptyView showWithIconName:iconName content:content touch:touchBlock];
    [view addSubview:emptyView];
    return emptyView;
}

- (void)showWithIconName:(NSString *)iconName content:(NSString *)content touch:(SimEmptyTouchBlock)touchBlock
{
    if (!_emptyLb) {
        _emptyLb = [[UILabel alloc] initWithFrame:self.bounds];
        _emptyLb.numberOfLines = 0;
        _emptyLb.backgroundColor = [UIColor clearColor];
        _emptyLb.font = [UIFont systemFontOfSize:16];
        _emptyLb.textColor = SimValue(kSimEmptyViewTitleColor, HEXRGBCOLOR(0x9ea0a3)) ;
        _emptyLb.textAlignment = NSTextAlignmentCenter;
        _emptyLb.lineBreakMode = NSLineBreakByWordWrapping;
        
    }
    _emptyLb.text = content;
    CGSize lbSize = SIM_MULTILINE_TEXTSIZE(_emptyLb.text, _emptyLb.font, CGSizeMake(self.width, CGFLOAT_MAX), _emptyLb.lineBreakMode);
    _emptyLb.size = lbSize;
    _emptyLb.centerX = self.width/2;

    UIImage *iconImage = nil;
    if (iconName.length > 0) {
        iconImage = SimUIImageNamed(iconName);
    }
    
    CGFloat contentHeight = iconImage.size.height + _emptyLb.height + kSimEmptyIconTextGap;
    if (iconImage == nil || contentHeight > self.height) {
        _emptyLb.frame = self.bounds;
        _emptyLb.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self addSubview:_emptyLb];
    }
    else{
        _contentView = [[UIView alloc] initWithFrame:self.bounds];
        _contentView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
        [self addSubview:_contentView];

        _iconView = [[UIImageView alloc] initWithImage:iconImage];
        _iconView.top = 0;
        _iconView.centerX = self.width/2;
        [_contentView addSubview:_iconView];
        
        _emptyLb.top = _iconView.bottom + kSimEmptyIconTextGap;
        [_contentView addSubview:_emptyLb];
       
        _contentView.height = _emptyLb.bottom;
        self.contentOffset = _contentOffset;
    }
    
    if (touchBlock) {
        self.touchBlock = touchBlock;
        UITapGestureRecognizer *tagGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap)];
        [self addGestureRecognizer:tagGR];
    }

}

- (void)setContentOffset:(CGFloat)contentOffset
{
    _contentOffset = contentOffset;
    if (_contentView) {
        _contentView.centerY = self.height/2 - contentOffset;
    }
}


- (void)handleTap
{
    NSMutableArray *array = [NSMutableArray arrayWithObjects:self, nil]; //retain Self
    [self removeFromSuperview];
    if (self.touchBlock) {
        self.touchBlock();
    }
    [array removeAllObjects];
}


@end
